<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>

        //给数组的原型上添加一个mySlice
        Array.prototype.mySlice = function (left, right) {
            right = right == undefined ? this.length : right
            var result = []  //[400,500]
            for (var i = left; i < right; i++) {
                result.push(this[i])
            }
            return result
        }

        var arr = [100, 200, 300, 400, 500, 600, 700, 800]
        var arr1 = [1, 2, 3, 4]
        console.log(arr.mySlice(3, 5))  //[400,500]
        console.log(arr.mySlice(2))     //[300, 400, 500, 600, 700, 800]


        //给数组的原型上添加一个myReverse
        var arr2 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
        //1.reverse是有返回值的，返回值为原数组的相反结果
        //2.reverse是改变原数组的，将原数组改编为之前的相反的结果
        Array.prototype.myReverse = function () {
            var left = 0
            var right = this.length - 1;
            while (left < right) {
                var t = this[left] // 1
                this[left] = this[right]
                this[right] = t
                left++;
                right--;
            }
            return this
        }
        console.log(arr2.myReverse())
        console.log(arr2)

        //如果涉及到双指针的问题，建议使用while循环  left  right



        //myConcat
        var arr4 = [1, 2, 3, 4]
        var arr5 = [100, 200, 300]
        Array.prototype.myConcat = function (data) {
            var result = [];
            for (var i = 0; i < this.length; i++) {
                result.push(this[i])
            }
            if (data.constructor == Number) {
                result.push(data)  //[1,2,3,4,1000]
            } else {
                for (var i = 0; i < data.length; i++) {
                    result.push(data[i])
                }
            }
            return result
        }
        console.log(arr4.myConcat(arr5))
        console.log(arr4.myConcat(1000))



        //forEach


        var arr6 = [100, 200, 300, 400, 500, 1, 2, 3, 4, 5, 6]
        // arr6.forEach(function (item, index) {
        //     item //被遍历的元素
        //     index //元素的下标
        // })

        Array.prototype.myForEach = function (callback) {
            for (var i = 0; i < this.length; i++) {
                callback(this[i], i)
            }
        }
        arr6.myForEach(function (item, index) {  //定义
            console.log(item, index)
        })


        //some
        var arr7 = [100, 200, 300, 400, 500, 600, 700]
        // console.log(
        //     arr7.some(function (item, index) {
        //         return item > 300//判断的条件
        //     })
        // )
        Array.prototype.mySome = function (callback) {
            for (var i = 0; i < this.length; i++) {
                if (callback(this[i], i)) {
                    //遍历结束
                    return true  //break ->  结束函数
                }
            }
            return false
        }
        console.log(arr7.mySome(function (item, index) {
            return item > 3000
        }))


        //every
    </script>
</body>

</html>